package com.qf.service.impl;

import com.qf.dao.GoodsDao;
import com.qf.dao.GoodsDaoImpl;
import com.qf.pojo.Goods;
import com.qf.pojo.PageBean;
import com.qf.service.GoodsService;
import com.qf.vo.GoodsVo;

import java.util.List;

/**
 * @author 千锋健哥
 */
public class GoodsServiceImpl implements GoodsService {

    @Override
    public List<GoodsVo> findList(String name, String pubDate) {
        GoodsDao goodsDao = new GoodsDaoImpl();
        List<GoodsVo> list = goodsDao.findGoodsList(name, pubDate);
        return list;
    }

    @Override
    public void add(Goods goods) {
        GoodsDao goodsDao = new GoodsDaoImpl();
        goodsDao.insert(goods);
    }

    @Override
    public PageBean<Goods> findPage(String goodsName, PageBean<Goods> pageBean) {
        //1. 计算从第几条开始查询
        Integer startRows = (pageBean.getCurrentPage() - 1) * pageBean.getPageSize();

        //2. 调用dao查询总条数
        GoodsDao goodsDao = new GoodsDaoImpl();
        long count = goodsDao.findPageCount(goodsName);

        //3. 调用dao查询列表数据
        List<Goods> list = goodsDao.findPage(goodsName, startRows, pageBean.getPageSize());

        //4. 封装pageBean分页对象
        pageBean.setTotalCount(count);
        pageBean.setList(list);

        //5. 返回结果
        return pageBean;
    }

    @Override
    public Goods detail(Integer id) {
        GoodsDao goodsDao = new GoodsDaoImpl();
        Goods goods = goodsDao.findById(id);
        return goods;
    }

}
